package Week15;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.blog.bean.BookMark;
import com.blog.bean.OffLineArticle;

import java.util.ArrayList;

public class DBService {
DBOpenHelper dbHelper;

public DBService(Context context){
	dbHelper=new DBOpenHelper(context);
}

/**
 * ����������ǩ
 */
 
public boolean saveMark(String id,String title,String link,int type,String summary,int commentCount){
	SQLiteDatabase db=dbHelper.getWritableDatabase();
   Cursor cursor= db.query("Mark", new String[]{"Id"}, "Id=?", new String[]{id}, null, null, null,null);
  
    if(cursor.getCount()>0){

    	return false;
    	
    }
    
	db.execSQL("insert into Mark values(?,?,?,?,?,?)",new Object[]{id,title,link,type,summary,commentCount});
	
	return true;
}


public void saveArticle(String id,String title,String summary){
	SQLiteDatabase db=dbHelper.getWritableDatabase();
	db.execSQL("insert into Article values(?,?,?)",new Object[]{id,title,summary});
}

public ArrayList<BookMark> getBookMarkList() {
	
	ArrayList<BookMark> bookMarkList=new ArrayList<BookMark>();
	
	SQLiteDatabase db=dbHelper.getWritableDatabase();
	Cursor cursor=db.query("Mark", null, null, null, null, null, null);
	while(cursor!=null&& cursor.moveToNext()){
		BookMark bookMark=new BookMark();
		bookMark.setId(cursor.getString(cursor.getColumnIndex("Id")));
		bookMark.setLink(cursor.getString(cursor.getColumnIndex("link")));
		bookMark.setTitle(cursor.getString(cursor.getColumnIndex("title")));
		bookMark.setType(cursor.getInt(cursor.getColumnIndex("type")));
		bookMark.setComment(cursor.getInt(cursor.getColumnIndex("comment")));
		bookMark.setSummary(cursor.getString(cursor.getColumnIndex("summary")));
		bookMarkList.add(bookMark);
		
	}
	db.close();
	return bookMarkList;
}


//ɾ����ǩ
public void deleteMark(String id) {
	SQLiteDatabase db=dbHelper.getWritableDatabase();
	if(id.equals(Constant.DELETE_ALL_MARK)){
		db.delete("Mark", null, null);
	}
	else{
		db.delete("Mark", "id=?", new String[]{id});
	}
	
}

//��ȡ�ղ��б�
public ArrayList<OffLineArticle> getOfflineList() {
	System.out.println("��ȡ�б�");
	ArrayList<OffLineArticle> offlineList=new ArrayList<OffLineArticle>();
	
	SQLiteDatabase db=dbHelper.getWritableDatabase();
	Cursor cursor=db.query("Article", null, null, null, null, null, null);
	
	while(cursor!=null && cursor.moveToNext()){
		OffLineArticle article=new OffLineArticle();
		article.setId(cursor.getString(cursor.getColumnIndex("Id")));
		article.setSummary(cursor.getString(cursor.getColumnIndex("summary")));
		article.setTitle(cursor.getString(cursor.getColumnIndex("title")));
		offlineList.add(article);
	}
	return offlineList;
}

public void deleteArticle(String id) {
	SQLiteDatabase db=dbHelper.getWritableDatabase();
	if(id.equals(Constant.DELETE_ALL_ARTICLE)){
		db.delete("Article", null, null);
	}
	else{
		db.delete("Article", "id=?", new String[]{id});
	}
	
}



}
